﻿package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.display.StageScaleMode;
	import flash.display.StageAlign;
	import flash.geom.Rectangle;
	
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;

	public class MultiAngleBounce extends Sprite
	{
		private var ball:Ball;	
		
		public var leftFlipper:Flipper;
		private var leftFlipAngle:Number = 30;
		private var leftFlipStartX:Number = 100;
		private var leftFlipStartY:Number = 300;
		
		public var rightFlipper:Flipper;
		private var rightFlipAngle:Number = -30;
		private var rightFlipStartX:Number = 300;
		private var rightFlipStartY:Number = 300;
		
		private var gravity:Number = 0.6;
		private var bounce:Number = -0.9;	
		
		public function MultiAngleBounce()
		{
			init();
		}
		
		private function init():void
		{
			//Make a ball.
			ball = new Ball(20);
			addChild(ball);
			ball.x = 250;
			ball.y = 50;
			
			//Create two flippers
			leftFlipper = new Flipper(leftFlipStartX, leftFlipStartY, leftFlipAngle, 1);
			
			rightFlipper = new Flipper(rightFlipStartX, rightFlipStartY, rightFlipAngle, 2);
			
			addChild(leftFlipper);
			addChild(rightFlipper);		
						
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
			
			stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
			stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);
		}
		
		private function onEnterFrame(event:Event):void
		{
			// normal motion code
			ball.vy += gravity;
			ball.x += ball.vx;
			ball.y += ball.vy;
			
			// bounce off ceiling, floor and walls
			if(ball.x + ball.radius > stage.stageWidth)
			{
				ball.x = stage.stageWidth - ball.radius;
				ball.vx *= bounce;
			}
			else if(ball.x - ball.radius < 0)
			{
				ball.x = ball.radius;
				ball.vx *= bounce;
			}
			if(ball.y + ball.radius > stage.stageHeight)
			{
				ball.y = stage.stageHeight - ball.radius;
				ball.vy *= bounce;
			}
			else if(ball.y - ball.radius < 0)
			{
				ball.y = ball.radius;
				ball.vy *= bounce;
			}
			
			// check each line			
			checkLine(leftFlipper, 1);
			
			checkLine(rightFlipper, 2);
			
		}
		
		private function checkLine(flipper:Flipper, side:Number):void
		{
			// get the bounding box of the line
			if(side == 1)
			{
				var bounds:Rectangle = new Rectangle(flipper.x, flipper.y, 50, 10);
			}
			if(side == 2)
			{
				var bounds:Rectangle = new Rectangle(flipper.x - 50, flipper.y - 10, 50, 10);
			}
			
			if(ball.x >= bounds.left && ball.x <= bounds.right)
			{
				
				// get angle, sine and cosine
				var angle:Number = flipper.angle;
				var cos:Number = Math.cos(angle);
				var sin:Number = Math.sin(angle);
				
				// get position of ball, relative to line
				var x1:Number = ball.x - flipper.x;
				var y1:Number = ball.y - flipper.y;
				
				// rotate coordinates
				var y2:Number = cos * y1 - sin * x1;
				
				// rotate velocity
				var vy1:Number = cos * ball.vy - sin * ball.vx;
				
				// perform bounce with rotated values
				if(y2 > -ball.height / 2 && y2 < vy1)
				{
					// rotate coordinates
					var x2:Number = cos * x1 + sin * y1;
					
					// rotate velocity
					var vx1:Number = cos * ball.vx + sin * ball.vy;
					
					if(y2 > -ball.height / 2 && y2 < vy1)
					{
						y2 = -ball.height / 2;
						vy1 *= bounce;
					}
					
					// rotate everything back;
					x1 = cos * x2 - sin * y2;
					y1 = cos * y2 + sin * x2;
					ball.vx = cos * vx1 - sin * vy1;
					ball.vy = cos * vy1 + sin * vx1;
					ball.x = flipper.x + x1;
					ball.y = flipper.y + y1;
				}
			}
		}
			
		public function keyDown(event:KeyboardEvent):void
		{		
			switch(event.keyCode)
			{
				case Keyboard.LEFT :				
					if(leftFlipper.rotation > 0)
					{
						leftFlipper.drawLeftFlipper(leftFlipper.rotation - 30);
					}									
				break;
			
				case Keyboard.RIGHT :					
					if(rightFlipper.rotation < 0)
					{
						rightFlipper.drawRightFlipper(rightFlipper.rotation + 30);
					}									
				break;
			}
		}
		
		public function keyUp(event:KeyboardEvent):void
		{		
			switch(event.keyCode)
			{
				case Keyboard.LEFT :				
				leftFlipper.drawLeftFlipper(leftFlipper.rotation + 30);
				break;
			
				case Keyboard.RIGHT :
				rightFlipper.drawRightFlipper(rightFlipper.rotation - 30);			
				break;
			}
		}		
	}
}